package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.dao.IFastNavigationDao;
import com.zhijiaoyun.model.FastNavigation;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.IFastNavigationService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "fastNavigationService")
public class FastNavigationServiceImpl implements IFastNavigationService {

	@Resource
	IFastNavigationDao fastNavigationDao;
	
	@Override
	public FastNavigation getFastNavigationByName(String name) {
		return fastNavigationDao.selectByName(name);
	}

	@Override
	public FastNavigation getFastNavigationByLinkUrl(String linkUrl) {
		return fastNavigationDao.selectByLinkUrl(linkUrl);
	}

	@Override
	public void addFastNavigation(FastNavigation fastNavigation) {
		fastNavigationDao.insert(fastNavigation);
	}

	@Override
	public void modifyFastNavigation(FastNavigation fastNavigation) {
		fastNavigationDao.updateByPrimaryKey(fastNavigation);
	}

	@Override
	public FastNavigation getNavigationByLinkUrlNotNavigationId(Long navigationId, String linkUrl) {
		return fastNavigationDao.selectByLinkUrlNotNavigationId(navigationId, linkUrl);
	}

	@Override
	public FastNavigation getNavigationByNameNotNavigationId(Long navigationId, String name) {
		return fastNavigationDao.selectByNameNotNavigationId(navigationId, name);
	}

	@Override
	public FastNavigation getFastNavigationByNavigationId(Long navigationId) {
		return fastNavigationDao.selectByPrimaryKey(navigationId);
	}

	@Override
	public PageList<FastNavigation> pageByParams(Integer pageIndex, String name, String linkUrl) {
		int count = fastNavigationDao.countByParams(name);
		PageData pageData = new PageData();
		List<FastNavigation> list = new ArrayList<>();
		PageList<FastNavigation> pageList = new PageList<>();
		if (count > 0) {
			int pageIndexInt = pageIndex == null ? 1 : pageIndex;
			pageData = PageData.createPage(count, pageIndexInt, DsConstant.PAGESIZE_DEFAULT);
			list = fastNavigationDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), name, linkUrl);
			pageList = new PageList<>(list, pageData);
		}
		return pageList;
	}

	@Override
	public void deleteFastNavigation(FastNavigation fastNavigation) {
		fastNavigationDao.deleteByPrimaryKey(fastNavigation.getNavigationId());
	}

}
